# coding:utf-8
module API::V3
  class Blacklists < Grape::API
    params do
      requires :mac, type: String, regexp: /\A([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}\z/
      optional :is_allowed, type: Boolean, default: false
    end
    resource :blacklists do
      get do
        # first check whether has special rules
        terminal = Terminal.find_by_mac(params[:mac])
        if terminal && !Blacklist.where(terminal_group_id: terminal.terminal_group_id).empty?
          blacklists = Blacklist.where(is_active: true, is_allowed: params[:is_allowed],
            terminal_group_id: terminal.terminal_group_id).map(&:address)
        else
          blacklists = Blacklist.where(is_active: true, is_allowed: params[:is_allowed]).map(&:address)
        end
        present blacklists
      end
    end
  end
end